package com.chainyoung.trademarket.ui;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.trademarket.R;

/**
 * Author by Damon,  on 2018/8/19.
 */
public class GuideActivity extends AppCompatActivity {


    ViewPager viewPager;

    private View[] views = new View[4];


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        viewPager = findViewById(R.id.view_pager);
        initUI();

    }

    protected void initUI() {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        views[0] = layoutInflater.inflate(R.layout.layout_guide_one, null);
        views[1] = layoutInflater.inflate(R.layout.layout_guide_two, null);
        views[2] = layoutInflater.inflate(R.layout.layout_guide_three, null);
        views[3] = layoutInflater.inflate(R.layout.layout_guide_four, null);
        views[3].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
//                SPUtil.getInstance().put("is_first", false);
//                startActivity(intent);
//                finish();
            }
        });

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 4;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(views[position], 0);
                return views[position];
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(views[position]);
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==3){
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            //execute the task
                            Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                            SPUtil.getInstance().put("is_first", false);
                            startActivity(intent);
                            finish();
                        }
                    }, 1000);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
